﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;
using NHibernate;

namespace Com.CMS263.DAO
{
    public class ArtTypeDAO
    {
        public bool Add(ArtType type)
        {

            DBHelper.TransInsert(type);
            return true;

        }
        public bool TrueDelete(int id)
        {
            DBHelper.ExecuteHQL(string.Format("delete from ArtType where id={0}", id));
            return true;
        }
        public bool Delete(int id)
        {
            DBHelper.DeleteByHQL(string.Format("delete from  ArtType  where id={0}", id));
            return true;
        }
        public bool Modify(ArtType type)
        {
            DBHelper.TransSaveOrUpdate(type);
            return false;
        }

        public IList<ArtType> GetAllTypeByIshow(int isShow)
        {
            return DBHelper.ExecuteHQL(string.Format("from ArtType where isShow={0}", isShow)).List<ArtType>();
        }
        public IList<ArtType> GetAllType()
        {
            ArtType a = new ArtType();
            
            return DBHelper.ExecuteHQL( "from ArtType  order by orderId asc ").List<ArtType>();
        }
       
        /// <summary>
        /// 得到某类的所有子类
        /// </summary>
        /// <param name="pid">类别编号</param>
        /// <param name="p">是否显示1为所有显示的0为所有不显示的，用于回收站</param>
        /// <returns></returns>
        public IList<ArtType> GetArtTypeByPid(int pid, int p)
        {
            IQuery iq = DBHelper.ExecuteHQL(string.Format("from ArtType  where pid={0} and isShow={1} order by orderId asc ", pid,p));
            if (iq != null)
            {
                return iq.List<ArtType>();
            }
            else
            {
                return null;
            }
        }

        public ArtType GetArtTypeById(int pid)
        {
            object obj = DBHelper.GetObject(typeof(ArtType), pid);
            if(obj!=null)
            {
             return  (ArtType)obj;
            }
            else
            {
                return null;
            }
        }

        public IList<ArtType> GetArtTypeByPid(int pid)
        {
            IQuery iq = DBHelper.ExecuteHQL(string.Format("from ArtType  where pid={0}  order by orderId asc ", pid));
            if (iq != null)
            {
                return iq.List<ArtType>();
            }
            else
            {
                return null;
            }
        }
    }
}
